using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Manazra.Models;
using Manazra.Models.Repository;
using Manazra.Helpers;
using Manazra.Controllers.Filters;

namespace Manazra.Controllers
{
    //viewmodel class for blog data
    public class BlogViewModel
    {
        //properties
        public Blog Blog { get; private set; }
        public IList<Article> Entries { get; private set; }

        //constructor
        public BlogViewModel(Blog blog)
        {
            Blog = blog;
        }
        public void SetEntries(IList<Article> entries)
        {
            Entries = entries;
        }
    }

    [BasePreControllerActionFilter]
    public class BlogsController : Controller
    {
        //blog repository
        private BlogRepository blogRep = new BlogRepository();

        private const int listingPageSize = 10;
        
        public ActionResult Index(int? page)
        {
            var blogs = blogRep.GetRecent();
            //apply paging
            var paginatedBlogs = new PaginatedList<Blog>(blogs, page ?? 0, listingPageSize);
            //show view
            return View(paginatedBlogs);
        }

        //details of a blog
        public ActionResult Details(string id)
        {
            int intId = Int32.Parse(id.Split('_')[0]);
            Blog blog = blogRep.GetById(intId);
            if (blog == null)
                return View("NotFound");
            else
            {
                //get all entries for this article
                var entries = blogRep.GetEntries(blog);
                //add them to blog view model
                BlogViewModel blogViewModel = new BlogViewModel(blog);
                blogViewModel.SetEntries(entries.ToList());
                //show view
                return View(blogViewModel);
            }
        }

        //create a new blog
        [AuthorizeFBOK]
        public ActionResult Create()
        {
            Blog blog = new Blog();
            return View(new BlogViewModel(blog));
        }

        //form post for saving new blog values
        [AcceptVerbs(HttpVerbs.Post), AuthorizeFBOK, ValidateInput(false)]
        public ActionResult Create(Blog blog, FormCollection formValues)
        {
            try
            {
                //get authentication object from the view data
                UserAuth UserAuth = (UserAuth)ViewData["UserAuth"];

                //set blog properties
                blog.Author = UserAuth.UserName;
                blog.CreatedDate = DateTime.Now;
                blog.UpdatedDate = DateTime.Now;
                blog.Title = formValues["Title"];

                //add new blog
                blogRep.Add(blog);

                //save changes
                blogRep.Save();
                return RedirectToAction("Details", new { id = blog.RecId });
            }
            catch
            {
                //use model state to show errors
                ModelState.AddRuleViolations(blog.GetRuleViolations());
                return View(new BlogViewModel(blog));
            }
        }

        //edit page for blog
        public ActionResult Edit(int id)
        {
            Blog blog = blogRep.GetById(id);
            if (blog == null)
                return View("NotFound");
            else
            {
                //get all entries for this article
                var entries = blogRep.GetEntries(blog);
                //add them to blog view model
                BlogViewModel blogViewModel = new BlogViewModel(blog);
                blogViewModel.SetEntries(entries.ToList());
                //show view
                return View(blogViewModel);
            }
        }

        //edit save page for blog
        [AcceptVerbs(HttpVerbs.Post), AuthorizeFBOK, ValidateInput(false)]
        public ActionResult Edit(Blog blog, FormCollection formValues)
        {
            if (blog == null)
                return View("NotFound");
            else
            {
                //get authentication object from the view data
                UserAuth UserAuth = (UserAuth)ViewData["UserAuth"];

                //make sure the author is making changes
                if (blog.IsAuthor(UserAuth.UserName))
                {
                    try
                    {
                        //set articles properties
                        blog.Title = formValues["Title"];
                        blog.UpdatedDate = DateTime.Now;

                        //save changes
                        blogRep.Save();

                        //redirect to details page
                        return RedirectToAction("Details", new { id = blog.RecId });
                    }
                    catch
                    {
                        //use model state to show errors
                        ModelState.AddRuleViolations(blog.GetRuleViolations());
                        //get all entries for this article
                        var entries = blogRep.GetEntries(blog);
                        //add them to blog view model
                        BlogViewModel blogViewModel = new BlogViewModel(blog);
                        blogViewModel.SetEntries(entries.ToList());
                        //show view
                        return View(blogViewModel);                        
                    }
                }
                else
                    return View("InvalidOwner");
            }
        }


    }
}